/*
 * CNV
 */
YAHOO.dasBrowser.genome.view.copyNumber.start = function(){
	// All the necessary function to start CNV view
	YAHOO.dasBrowser.genome.view.copyNumber.drawCopyNumberViewStructure();
}



/*
 * Draw layout for the Sequence view
 */
YAHOO.dasBrowser.genome.view.copyNumber.drawCopyNumberViewStructure = function(){

	var panelMarginBottom = YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.panelContainer.marginBottom;
	var panelMarginTop = YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.panelContainer.marginTop;
	var panelMarginLeft = YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.panelContainer.marginLeft;
	
	var viewPrefix = "CNVv";
	var panelContainerPrefix = YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.panelContainer.labelPrefix;
	
	var repMarginLeft = YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.panelContainer.chromosomeContainer.leftPaddingPx;
	var chromosomeContainerPrefix = YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.panelContainer.chromosomeContainer.labelPrefix;

	var container = new YAHOO.util.Element('copyNumberView');

	var panel = document.createElement("div");
	var ePanel = new YAHOO.util.Element(panel);
	var ePanelPrefix = viewPrefix + panelContainerPrefix;
	ePanel.set('id', ePanelPrefix);
	ePanel.setStyle('margin-bottom', YAHOO.dasBrowser.util.addPxSuffix(panelMarginBottom));
	ePanel.setStyle('margin-top', YAHOO.dasBrowser.util.addPxSuffix(panelMarginTop));
	ePanel.setStyle('margin-left', YAHOO.dasBrowser.util.addPxSuffix(panelMarginLeft));
	//ePanel.appendChild(document.createTextNode("Hello"));
	
	container.appendChild(ePanel);
	drawTree();
}

function drawTree(){
	//
	/*
	var treeData = [
	
			'Label 0',
			{type:'Text', label:'text label 1', title:'this is the tooltip for text label 1'},
			{type:'Text', label:'branch 1', title:'there should be children here', expanded:true, children:[
				'Label 1-0'
			]},
			{type:'Text',label:'YAHOO',title:'this should be an href', href:'http://www.yahoo.com', target:'somewhere new'},
			{type:'HTML',html:'<a href="developer.yahoo.com/yui">YUI</a>'},
			{type:'MenuNode',label:'branch 3',title:'this is a menu node', expanded:false, children:[
				'Label 3-0',
				'Label 3-1'
			]}
		];
	*/
	//var treeData = [];

	//YAHOO.dasBrowser.genome.view.copyNumber.treeData = treeData;
	//YAHOO.dasBrowser.genome.view.copyNumber.tree = new YAHOO.widget.TreeView("CNVvPanel",treeData);
	//var root = YAHOO.dasBrowser.genome.view.copyNumber.tree.getRoot(); 
	//var tmpNode = new YAHOO.widget.TextNode({label: "rafa", expanded: false}, root); 
	//tmpNode = new YAHOO.widget.TextNode({label: "rafa2", expanded: false}, root.children[5]); 
	
	/*
	//var node = root.children[5].children;
	var node = root;
	for (var j = 0; j < node.length; j++) {
		//console.log(node.children[j].label);
		console.log("1: "+j);
	}	
	*/
	/*
	for (var j = 0; j < root.children.length; j++) {
		console.log(root.children[j].label);
		console.log("2: "+j);
	}
	*/	
	
	
	
	
	/* 
						if(root.children.length == 0){
						tmpNode = new YAHOO.widget.TextNode({label: featureTypeId, expanded: true}, root); 
					} else {
						for (var m = 0; m < root.children.length; m++) {
							if(root.children[m].label == featureTypeId){
								if(root.children[m].children.length == 0){
									tmpNode = new YAHOO.widget.TextNode({label: featureTypeCategory, expanded: true}, root.children[m]); 
								} else {
									for (var n = 0; n < root.children[m].children.length; n++) {
										if(root.children[m].children[n].label == featureTypeCategory){
											//for
										} else {
											tmpNode = new YAHOO.widget.TextNode({label: featureTypeCategory, expanded: true}, root.children[m]);
										}
									}
								}
							} else {
								tmpNode = new YAHOO.widget.TextNode({label: featureTypeId, expanded: true}, root);
							}
						}
					}
	 */
	
		/*
	TYPE; copynumber
		CATEGORY; array000001
			METHOD; DNAcopy
				SEGMENT; 3
					FEATURE ID/LABEL (SCORE):LINK; block0007 (1.79):link
						NOTES; PTRU:1.770
	*/
	
//myTreeElement()
	//AQUI ME QUEDE
	
	var treeData = [];
	var cnvSources = YAHOO.dasBrowser.genome.annotations.sources;
	for (var j = 0; j < cnvSources.length; j++) {
		for (var i in cnvSources[j].result) {
			if (i) {
			//if (i == "chromosome1" || i == "chromosome2") {
				//console.log(cnvSources[j].result[i].kv.features.length);
				for (var p = 0; p < cnvSources[j].result[i].kv.features.length; p++) {
					var annotationFeature = cnvSources[j].result[i].kv.features[p];
					//printFeature(annotationFeature);
					var featureTypeId = annotationFeature.getTypeId();
					var featureTypeCategory = annotationFeature.getTypeCategory();
					var featureMethodId = annotationFeature.getMethodId();
					var featureSegment = i;
					var featureId = annotationFeature.getId();
					var featureNote = annotationFeature.getNote();
					var featureNoteArray = featureNote.split(", ");
					
					var elementReference = new Object();
					elementReference.typeId = null;
					elementReference.typeCategory = null;
					elementReference.methodId = null;
					elementReference.depperLevel = null;
					elementReference.segment = null;
					elementReference.id = null;
					//elementReference.note = null;
					
					
					
					elementReference.depperLevel = "featureTypeId";
					featureTypeIdLoop:
					for (var m = 0; m < treeData.length; m++) {
						if (treeData[m].label == featureTypeId) {
							elementReference.typeId = m;
							elementReference.depperLevel = "featureTypeCategory";
							featureTypeCategoryLoop:
							for (var n = 0; n < treeData[m].children.length; n++) {
								if (treeData[m].children[n].label == featureTypeCategory) {
									elementReference.typeCategory = n;
									elementReference.depperLevel = "featureMethodId";
									featureMethodIdLoop:
									for (var o = 0; o < treeData[m].children[n].children.length; n++) {
										if (treeData[m].children[n].children[o].label == featureMethodId) {
											elementReference.methodId = o;
											elementReference.depperLevel = "featureSegment";
											featureSegmentLoop:
											for (var q = 0; q < treeData[m].children[n].children[o].children.length; q++) {
												if (treeData[m].children[n].children[o].children[q].label == i) {
													elementReference.segment = q;
													elementReference.depperLevel = "featureId";
													featureIdLoop:
													for (var r = 0; r < treeData[m].children[n].children[o].children[q].children.length; r++) {
														if (treeData[m].children[n].children[o].children[q].children[r].label == featureId) {
															elementReference.Id = r;
															//Loop
															break featureIdLoop;
														}
													}
													break featureSegmentLoop;
												}
											}
											break featureMethodIdLoop;
										}
									}
									break featureTypeCategoryLoop;
								}
							}
							break featureTypeIdLoop;
						}
					}

					//YAHOO.dasBrowser.util.displayObjProp(elementReference);

					if(elementReference.depperLevel == null){
						// Already in the tree. No insert.
					} else {
						if(elementReference.depperLevel == "featureTypeId" && elementReference.typeId == null){
							var featureTypeIdElement = new myTreeElement();
							featureTypeIdElement.setLabel(featureTypeId);
							featureTypeIdElement.setExpanded(true);
							
							var featureTypeCategoryElement = new myTreeElement();
							featureTypeCategoryElement.setLabel(featureTypeCategory);
							featureTypeCategoryElement.setExpanded(true);
							
							var featureMethodIdElement = new myTreeElement();
							featureMethodIdElement.setLabel(featureMethodId);
							featureMethodIdElement.setExpanded(true);
							
							var featureSegmentElement = new myTreeElement();
							featureSegmentElement.setLabel(featureSegment);
							featureSegmentElement.setExpanded(false);
							
							var featureIdElement = new myTreeElement();
							featureIdElement.setLabel(featureId);
							featureIdElement.setExpanded(true);
							
							for (var t = 0; t < featureNoteArray.length; t++) {
								var featureNoteElement = new myTreeElement();
								featureNoteElement.setLabel(featureNoteArray[t]);
								featureNoteElement.setExpanded(true);
								featureIdElement.addChildren(featureNoteElement);
							}
							
							featureSegmentElement.addChildren(featureIdElement);
							featureMethodIdElement.addChildren(featureSegmentElement);
							featureTypeCategoryElement.addChildren(featureMethodIdElement);
							featureTypeIdElement.addChildren(featureTypeCategoryElement);
							treeData.push(featureTypeIdElement);
							
						} else if(elementReference.depperLevel == "featureTypeCategory" && elementReference.typeCategory == null){
							var featureTypeCategoryElement = new myTreeElement();
							featureTypeCategoryElement.setLabel(featureTypeCategory);
							//featureTypeCategoryElement.setExpanded(true);
							
							var featureMethodIdElement = new myTreeElement();
							featureMethodIdElement.setLabel(featureMethodId);
							//featureMethodIdElement.setExpanded(true);
							
							var featureSegmentElement = new myTreeElement();
							featureSegmentElement.setLabel(featureSegment);
							featureSegmentElement.setExpanded(false);
							
							var featureIdElement = new myTreeElement();
							featureIdElement.setLabel(featureId);
							//featureIdElement.setExpanded(true);
							
							for (var t = 0; t < featureNoteArray.length; t++) {
								var featureNoteElement = new myTreeElement();
								featureNoteElement.setLabel(featureNoteArray[t]);
								//featureNoteElement.setExpanded(true);
								featureIdElement.addChildren(featureNoteElement);
							}
							
							featureSegmentElement.addChildren(featureIdElement);
							featureMethodIdElement.addChildren(featureSegmentElement);
							featureTypeCategoryElement.addChildren(featureMethodIdElement);
							treeData[elementReference.typeId].addChildren(featureTypeCategoryElement);

							
						} else if(elementReference.depperLevel == "featureMethodId" && elementReference.methodId == null){
							var featureMethodIdElement = new myTreeElement();
							featureMethodIdElement.setLabel(featureMethodId);
							//featureMethodIdElement.setExpanded(true);
							
							var featureSegmentElement = new myTreeElement();
							featureSegmentElement.setLabel(featureSegment);
							featureSegmentElement.setExpanded(false);
							
							var featureIdElement = new myTreeElement();
							featureIdElement.setLabel(featureId);
							//featureIdElement.setExpanded(true);
							
							for (var t = 0; t < featureNoteArray.length; t++) {
								var featureNoteElement = new myTreeElement();
								featureNoteElement.setLabel(featureNoteArray[t]);
								//featureNoteElement.setExpanded(true);
								featureIdElement.addChildren(featureNoteElement);
							}
							
							featureSegmentElement.addChildren(featureIdElement);
							featureMethodIdElement.addChildren(featureSegmentElement);
							treeData[elementReference.typeId].children[elementReference.typeCategory].addChildren(featureMethodIdElement);
							
							
						} else if(elementReference.depperLevel == "featureSegment" && elementReference.segment == null){
							var featureSegmentElement = new myTreeElement();
							featureSegmentElement.setLabel(featureSegment);
							featureSegmentElement.setExpanded(false);
							
							var featureIdElement = new myTreeElement();
							featureIdElement.setLabel(featureId);
							//featureIdElement.setExpanded(true);
							
							for (var t = 0; t < featureNoteArray.length; t++) {
								var featureNoteElement = new myTreeElement();
								featureNoteElement.setLabel(featureNoteArray[t]);
								//featureNoteElement.setExpanded(true);
								featureIdElement.addChildren(featureNoteElement);
							}
							
							featureSegmentElement.addChildren(featureIdElement);
							treeData[elementReference.typeId].children[elementReference.typeCategory].children[elementReference.methodId].addChildren(featureSegmentElement);
						
						} else if(elementReference.depperLevel == "featureId" && elementReference.id == null){
							var featureIdElement = new myTreeElement();
							featureIdElement.setLabel(featureId);
							//featureIdElement.setExpanded(true);
							
							for (var t = 0; t < featureNoteArray.length; t++) {
								var featureNoteElement = new myTreeElement();
								featureNoteElement.setLabel(featureNoteArray[t]);
								//featureNoteElement.setExpanded(true);
								featureIdElement.addChildren(featureNoteElement);
							}
							
							treeData[elementReference.typeId].children[elementReference.typeCategory].children[elementReference.methodId].children[elementReference.segment].addChildren(featureIdElement);
						}
						
						//featureSegment
					}
				}
			}
		}
	}
	YAHOO.dasBrowser.genome.view.copyNumber.tree = new YAHOO.widget.TreeView("CNVvPanel",treeData);
	var root = YAHOO.dasBrowser.genome.view.copyNumber.tree.getRoot(); 
	
	/*
	var treeData = [
	
			'Label 0',
			{type:'Text', label:'text label 1', title:'this is the tooltip for text label 1'},
			{type:'Text', label:'branch 1', title:'there should be children here', expanded:true, children:[
				'Label 1-0'
			]},
			{type:'Text',label:'YAHOO',title:'this should be an href', href:'http://www.yahoo.com', target:'somewhere new'},
			{type:'HTML',html:'<a href="developer.yahoo.com/yui">YUI</a>'},
			{type:'MenuNode',label:'branch 3',title:'this is a menu node', expanded:false, children:[
				'Label 3-0',
				'Label 3-1'
			]}
		];
		*/
	
	//tmpNode = new YAHOO.widget.TextNode({label: featureTypeCategory, expanded: true}, root.children[featureTypeIdPresent]); 
	//tmpNode = new YAHOO.widget.TextNode({label: featureTypeId, expanded: true}, root); 
	
	YAHOO.dasBrowser.genome.view.copyNumber.tree.render();
	//YAHOO.dasBrowser.util.displayObjProp(treeData[4]);
}

function printFeature(annotationFeature){
					//console.log(annotationFeature.getTypeId());
					//console.log(annotationFeature.getTypeCategory());
					//console.log(annotationFeature.getMethodId());
					//console.log(annotationFeature.getId());
					//console.log(annotationFeature.getNote());
}
